package pyip.lib.javalearn.io.nio;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.CharBuffer;
import java.util.Scanner;

public class BioTcpClient2 {

    public static void main(String[] args) throws IOException {

        String host = "127.0.0.1";
        int port = 9999;
        Socket socket = new Socket(host, port);

        System.out.println("客户端起动");

        while (true) {
            Scanner in = new Scanner(System.in);
            String message = in.nextLine();
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            pw.write(message);
            pw.flush();

            InputStreamReader reader = new InputStreamReader(socket.getInputStream());
            CharBuffer buf = CharBuffer.allocate(100);
            reader.read(buf);
            buf.flip();
            System.out.println("client:" + String.valueOf(buf));

            if (message.equals("quit")) {
                break;
            }
            buf.clear();
        }
        socket.close();
    }
}
